<?php

	namespace org\tekuna\demo\weblog\action;
	
	use org\tekuna\framework\filter\data\IntegerFilter;

	use \PDOException;
	
	use org\tekuna\demo\weblog\manager\ArticleManager;
	
	use org\tekuna\framework\request\Request;
	use org\tekuna\framework\action\ActionEvent;
	
	use org\tekuna\plugin\simpletemplate\SimpleTemplateAction;
	
	
	class ListArticlesArchiveAction extends SimpleTemplateAction {
		
		private $objArticleManager;
		
		protected
			$iYear = 0,
			$iMonth = 0,
			$arrArticles = array();
		
		
		public function executeTemplate(ActionEvent $objActionEvent, Request $objRequest) {
			
			// set the filter for the article key
			$objActionEvent -> setPartFilter('year', new IntegerFilter());
			$objActionEvent -> setPartFilter('month', new IntegerFilter());
			
			// get the article key param
			$this -> iYear = $objActionEvent -> getUrlPart('year');
			$this -> iMonth = $objActionEvent -> getUrlPart('month');
			
			// set a new application title
			$sBasicTitle = $this -> getApplicationContext() -> getAppTitle();
			$this -> getApplicationContext() -> setAppTitle("Article archives for $this->iMonth/$this->iYear - $sBasicTitle");
			
			try {
				
				// load archive articles to the array
				// TODO: pagination
				$this -> arrArticles = $this -> objArticleManager -> getArchiveArticles($this -> iYear, $this -> iMonth);
			}
			catch (PDOException $objException) {
				
				// nothing, handled in template when no articles found.
			}
		}
		
		public function setArticleManager(ArticleManager $objArticleManager) {
			
			$this -> objArticleManager = $objArticleManager;
		}
	}
